package com.ihrm.system.service;


import com.ihrm.common.utils.IdWorker;

import com.ihrm.domain.system.User;
import com.ihrm.system.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private IdWorker idWorker;
    /*
     * 1.保存用戶
     * */
    public  void  save(User user){
        //设置主键的值
        String id= idWorker.nextId()+ "";
        user.setPassword("123456");
        user.setEnableState(1);
        user.setId(id);
        //调用dao保存用戶
        userDao.save(user);
    }
    /*
     * 2.更新用戶
     * */
    public  void  update(User user){
        //根据id查询用戶
        User usr = userDao.findById(user.getId()).get();

        //设置用戶属性
        usr.setUsername(user.getUsername());
        usr.setPassword(user.getPassword());
        usr.setDepartmentName(user.getDepartmentName());
        usr.setDepartmentId(user.getDepartmentId());
        //更新用戶
        userDao.save(usr);
    }

    /*
     * 3..ID查询用戶
     * */
    public User  findById(String id){
        return  userDao.findById(id).get();
    }
    /*
     * 4.查询全部用戶
     * 參數:map集合的形式
     * */
    public Page findAll(Map<String,Object> map,int page,int size){
        //1.需要查詢的條件
        Specification spec=new Specification() {
            /**
             * 动态拼接查询
             */
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                //根据请求的companyId是否为空构造查询条件
                if(!StringUtils.isEmpty(map.get("companyIId"))){
                    list.add(cb.equal(root.get("companyId").as(String.class),(String)map.get("companyId")));

                }
                //根据请求的部门ID来构造查询条件
                if(!StringUtils.isEmpty(map.get("departmentId"))){
                    list.add(cb.equal(root.get("departmentId").as(String.class),(String)map.get("departmentId")));

                }
                //根据请求的hasdept判断是否
               // 分配
                if(!StringUtils.isEmpty(map.get("hasDept"))||"0".equals((String)map.get("hasDept"))){
                    list.add(cb.isNull(root.get("departmentId")));
                }else{
                    list.add(cb.isNotNull(root.get("departmentId")));
                }
                return cb.and(list.toArray(new Predicate[list.size()]));
            }
        };
        //分页
        Page pageUser = userDao.findAll(spec, new PageRequest(page-1, size));
        return pageUser;
    }
    /*
     * 5.删除用戶
     * */
    public  void  deleteById(String id){
        userDao.deleteById(id);
    }
}